package com.music.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageInfo;
import com.music.dto.Result;
import com.music.entity.Singer;
import com.music.entity.Song;
import com.music.service.SongService;
import com.music.task.TopListTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.*;

@RestController
@RequestMapping("/song")
public class SongController {
    @Autowired
    private SongService songService;
    @Autowired
    private TopListTask topListTask;
    @GetMapping
    public Result doFindOrderByTime(Integer songDistrictId){
        Result result=new Result();
        List<Song> list=new ArrayList<Song>();
        Song song=new Song();
        if(songDistrictId!=null&&!"".equals(songDistrictId)){
            song.setSongDistrictId(songDistrictId);
        }
        list=songService.SelectOrderByTime(song);
        if(!list.isEmpty()){
            result.ok(list);
        }else{
            result.fail(0,"没有相关数据");
        }
        return result;
    }
    @RequestMapping("/change")
    public Result dochange() throws JsonProcessingException {
        Result result=new Result();
        Double a[][]=topListTask.change();
        List<Song> songList=new ArrayList<Song>();
        if(a!=null){
            for(int i=0;i<a.length;i++){
                Song song=songService.fingById(String.valueOf(Math.round(a[i][1])));
                songList.add(song);
            }
            result.ok(songList);
        }else{
            result.fail(0,"失败");
        }
        return result;
    }
    @RequestMapping("/more")
    public Result doget(String new1, String hot,Integer songDistrictId){
        Result result=new Result();
        Map<String,Object> map = new HashMap<>();
        if("true".equals(new1)){
            map.put("new1",true);
        }else{
            map.put("new1","");
        }
        if("true".equals(hot)){
            map.put("hot",true);
        }else{
            map.put("hot","");
        }
        if(songDistrictId!=null){
            map.put("songDistrictId",songDistrictId);
        }

        List<Song> list=songService.findBycondition(map);
        if(!list.isEmpty()){
            result.ok(list);
        }else{
            result.fail(0,"没有相关数据");
        }
        return result;
    }


    /**
     * 林鑫海
     * @param songName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/songPageList")
    public @ResponseBody
    Result getPageSinger(String songName, int pageNum, int pageSize){
        Result result = new Result();
        Song song = new Song();
        song.setSongName(songName);
        PageInfo<Song> songByPage = songService.getSongByPage(pageNum, pageSize, song);
        if (songByPage != null){
            result.ok(songByPage);
        }else {
            result.fail(0,"无数据");
        }
        return result;
    }


    @PostMapping("/addSong")
    public @ResponseBody Result addOperateSinger(@RequestParam String songId,@RequestParam String songName,@RequestParam Integer songStatus, @RequestParam String songDuration,@RequestParam String singerId,@RequestParam String albumId){
        Result result = new Result();
        Song song = new Song();
        song.setSongId(songId);
        song.setSongName(songName);
        song.setSongStatus(songStatus);
        song.setSongDuration(songDuration);
        song.setSingerId(singerId);
        song.setAlbumId(albumId);
        //调用业务层
        boolean b = songService.addSong(song);
        if (b){
            result.ok(true).setMessage("添加成功");
        }else {
            result.fail(0,"添加失败,可能原因是它已存在");
        }
        return result;
    }

    @PostMapping("/updateSong")
    public @ResponseBody Result updateOperateSinger(@RequestParam String songId,@RequestParam String songName,@RequestParam Integer songStatus, @RequestParam String songDuration,@RequestParam String singerId,@RequestParam String albumId){
        Result result = new Result();
        Song song = new Song();
        song.setSongId(songId);
        song.setSongName(songName);
        song.setSongStatus(songStatus);
        song.setSongDuration(songDuration);
        song.setSingerId(singerId);
        song.setAlbumId(albumId);
        //调用业务层
        boolean m = songService.modifySong(song);
        if (m){
            result.ok(true).setMessage("修改成功");
        }else {
            result.fail(0,"修改失败,可能原因是它不存在");
        }
        return result;
    }

    @PostMapping("/getSongById")
    public @ResponseBody Result getSongById(@RequestParam String songId) {
        Result result = new Result();
        Song song=songService.getById(songId);
        if (song!=null){
            result.ok(song);
        }else{
            result.fail(0,"没找到相关数据");
        }
        return result;
    }
}
